package com.weiyi.portal.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weiyi.portal.modules.ums.model.UmsMember;
import com.weiyi.portal.modules.ums.model.UmsNews;
import com.weiyi.portal.modules.ums.mapper.UmsNewsMapper;
import com.weiyi.portal.modules.ums.service.UmsMemberService;
import com.weiyi.portal.modules.ums.service.UmsNewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PZC
 * @since 2022-10-30
 */
@Service
public class UmsNewsServiceImpl extends ServiceImpl<UmsNewsMapper, UmsNews> implements UmsNewsService {

    @Autowired
    UmsNewsService umsNewsService;
    @Autowired
    UmsMemberService umsMemberService;
    @Override
    public Boolean saveNews(UmsNews news) {
        news.setId(null);
        news.setShowStatus(1);
        news.setCreateTime(new Date());
        return saveOrUpdate(news);
    }

    @Override
    public List<UmsNews> getNewsById(Integer id) {
        UmsMember member = umsMemberService.getById(id);
        QueryWrapper<UmsNews>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UmsNews::getAuthorId, id);
        List<UmsNews> news = list(queryWrapper);
        return news;
    }

    /**
     * 获取用户发布动态数
     * @param id
     * @return
     */
    @Override
    public Integer getNewsNum(Integer id) {
        QueryWrapper<UmsNews> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("sum(author_id) as newsNum")
                .lambda()
                .eq(UmsNews::getAuthorId, id);

        Map<String, Object> map = umsNewsService.getMap(queryWrapper);
        Integer newsNum= (Integer) map.get("newsNum");
        return newsNum;
    }

    @Override
    public List<UmsNews> getDraft(Integer id) {
        QueryWrapper<UmsNews>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsNews::getAuthorId, id);
        List<UmsNews> draft = list(queryWrapper);
        return draft;
    }
}
